﻿using System;
using System.Data;
using System.Text;
using System.Data.OleDb;
using Maticsoft.DBUtility;//Please add references
namespace lcps.DAL
{
	/// <summary>
	/// 数据访问类:supplyplan
	/// </summary>
	public partial class supplyplan
	{
		public supplyplan()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperOleDb.GetMaxID("ID", "supplyplan"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int ID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from supplyplan");
			strSql.Append(" where ID=@ID");
			OleDbParameter[] parameters = {
					new OleDbParameter("@ID", OleDbType.Integer,4)
			};
			parameters[0].Value = ID;

			return DbHelperOleDb.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(lcps.Model.supplyplan model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into supplyplan(");
			strSql.Append("xh,plandate,projectname,projectid,wbs,xyunit,materuser,projectunit,projectmajor,plannum,planoverdate,addplannum)");
			strSql.Append(" values (");
			strSql.Append("@xh,@plandate,@projectname,@projectid,@wbs,@xyunit,@materuser,@projectunit,@projectmajor,@plannum,@planoverdate,@addplannum)");
			OleDbParameter[] parameters = {
					new OleDbParameter("@xh", OleDbType.VarChar,255),
					new OleDbParameter("@plandate", OleDbType.Date),
					new OleDbParameter("@projectname", OleDbType.VarChar,255),
					new OleDbParameter("@projectid", OleDbType.VarChar,255),
					new OleDbParameter("@wbs", OleDbType.VarChar,255),
					new OleDbParameter("@xyunit", OleDbType.VarChar,255),
					new OleDbParameter("@materuser", OleDbType.VarChar,255),
					new OleDbParameter("@projectunit", OleDbType.VarChar,255),
					new OleDbParameter("@projectmajor", OleDbType.VarChar,255),
					new OleDbParameter("@plannum", OleDbType.VarChar,255),
					new OleDbParameter("@planoverdate", OleDbType.Date),
					new OleDbParameter("@addplannum", OleDbType.VarChar,255)};
			parameters[0].Value = model.xh;
			parameters[1].Value = model.plandate;
			parameters[2].Value = model.projectname;
			parameters[3].Value = model.projectid;
			parameters[4].Value = model.wbs;
			parameters[5].Value = model.xyunit;
			parameters[6].Value = model.materuser;
			parameters[7].Value = model.projectunit;
			parameters[8].Value = model.projectmajor;
			parameters[9].Value = model.plannum;
			parameters[10].Value = model.planoverdate;
			parameters[11].Value = model.addplannum;

			int rows=DbHelperOleDb.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(lcps.Model.supplyplan model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update supplyplan set ");
			strSql.Append("xh=@xh,");
			strSql.Append("plandate=@plandate,");
			strSql.Append("projectname=@projectname,");
			strSql.Append("projectid=@projectid,");
			strSql.Append("wbs=@wbs,");
			strSql.Append("xyunit=@xyunit,");
			strSql.Append("materuser=@materuser,");
			strSql.Append("projectunit=@projectunit,");
			strSql.Append("projectmajor=@projectmajor,");
			strSql.Append("plannum=@plannum,");
			strSql.Append("planoverdate=@planoverdate,");
			strSql.Append("addplannum=@addplannum");
			strSql.Append(" where ID=@ID");
			OleDbParameter[] parameters = {
					new OleDbParameter("@xh", OleDbType.VarChar,255),
					new OleDbParameter("@plandate", OleDbType.Date),
					new OleDbParameter("@projectname", OleDbType.VarChar,255),
					new OleDbParameter("@projectid", OleDbType.VarChar,255),
					new OleDbParameter("@wbs", OleDbType.VarChar,255),
					new OleDbParameter("@xyunit", OleDbType.VarChar,255),
					new OleDbParameter("@materuser", OleDbType.VarChar,255),
					new OleDbParameter("@projectunit", OleDbType.VarChar,255),
					new OleDbParameter("@projectmajor", OleDbType.VarChar,255),
					new OleDbParameter("@plannum", OleDbType.VarChar,255),
					new OleDbParameter("@planoverdate", OleDbType.Date),
					new OleDbParameter("@addplannum", OleDbType.VarChar,255),
					new OleDbParameter("@ID", OleDbType.Integer,4)};
			parameters[0].Value = model.xh;
			parameters[1].Value = model.plandate;
			parameters[2].Value = model.projectname;
			parameters[3].Value = model.projectid;
			parameters[4].Value = model.wbs;
			parameters[5].Value = model.xyunit;
			parameters[6].Value = model.materuser;
			parameters[7].Value = model.projectunit;
			parameters[8].Value = model.projectmajor;
			parameters[9].Value = model.plannum;
			parameters[10].Value = model.planoverdate;
			parameters[11].Value = model.addplannum;
			parameters[12].Value = model.ID;

			int rows=DbHelperOleDb.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from supplyplan ");
			strSql.Append(" where ID=@ID");
			OleDbParameter[] parameters = {
					new OleDbParameter("@ID", OleDbType.Integer,4)
			};
			parameters[0].Value = ID;

			int rows=DbHelperOleDb.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string IDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from supplyplan ");
			strSql.Append(" where ID in ("+IDlist + ")  ");
			int rows=DbHelperOleDb.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public lcps.Model.supplyplan GetModel(int ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ID,xh,plandate,projectname,projectid,wbs,xyunit,materuser,projectunit,projectmajor,plannum,planoverdate,addplannum from supplyplan ");
			strSql.Append(" where ID=@ID");
			OleDbParameter[] parameters = {
					new OleDbParameter("@ID", OleDbType.Integer,4)
			};
			parameters[0].Value = ID;

			lcps.Model.supplyplan model=new lcps.Model.supplyplan();
			DataSet ds=DbHelperOleDb.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["ID"]!=null && ds.Tables[0].Rows[0]["ID"].ToString()!="")
				{
					model.ID=int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["xh"]!=null && ds.Tables[0].Rows[0]["xh"].ToString()!="")
				{
					model.xh=ds.Tables[0].Rows[0]["xh"].ToString();
				}
				if(ds.Tables[0].Rows[0]["plandate"]!=null && ds.Tables[0].Rows[0]["plandate"].ToString()!="")
				{
					model.plandate=DateTime.Parse(ds.Tables[0].Rows[0]["plandate"].ToString());
				}
				if(ds.Tables[0].Rows[0]["projectname"]!=null && ds.Tables[0].Rows[0]["projectname"].ToString()!="")
				{
					model.projectname=ds.Tables[0].Rows[0]["projectname"].ToString();
				}
				if(ds.Tables[0].Rows[0]["projectid"]!=null && ds.Tables[0].Rows[0]["projectid"].ToString()!="")
				{
					model.projectid=ds.Tables[0].Rows[0]["projectid"].ToString();
				}
				if(ds.Tables[0].Rows[0]["wbs"]!=null && ds.Tables[0].Rows[0]["wbs"].ToString()!="")
				{
					model.wbs=ds.Tables[0].Rows[0]["wbs"].ToString();
				}
				if(ds.Tables[0].Rows[0]["xyunit"]!=null && ds.Tables[0].Rows[0]["xyunit"].ToString()!="")
				{
					model.xyunit=ds.Tables[0].Rows[0]["xyunit"].ToString();
				}
				if(ds.Tables[0].Rows[0]["materuser"]!=null && ds.Tables[0].Rows[0]["materuser"].ToString()!="")
				{
					model.materuser=ds.Tables[0].Rows[0]["materuser"].ToString();
				}
				if(ds.Tables[0].Rows[0]["projectunit"]!=null && ds.Tables[0].Rows[0]["projectunit"].ToString()!="")
				{
					model.projectunit=ds.Tables[0].Rows[0]["projectunit"].ToString();
				}
				if(ds.Tables[0].Rows[0]["projectmajor"]!=null && ds.Tables[0].Rows[0]["projectmajor"].ToString()!="")
				{
					model.projectmajor=ds.Tables[0].Rows[0]["projectmajor"].ToString();
				}
				if(ds.Tables[0].Rows[0]["plannum"]!=null && ds.Tables[0].Rows[0]["plannum"].ToString()!="")
				{
					model.plannum=ds.Tables[0].Rows[0]["plannum"].ToString();
				}
				if(ds.Tables[0].Rows[0]["planoverdate"]!=null && ds.Tables[0].Rows[0]["planoverdate"].ToString()!="")
				{
					model.planoverdate=DateTime.Parse(ds.Tables[0].Rows[0]["planoverdate"].ToString());
				}
				if(ds.Tables[0].Rows[0]["addplannum"]!=null && ds.Tables[0].Rows[0]["addplannum"].ToString()!="")
				{
					model.addplannum=ds.Tables[0].Rows[0]["addplannum"].ToString();
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ID,xh,plandate,projectname,projectid,wbs,xyunit,materuser,projectunit,projectmajor,plannum,planoverdate,addplannum ");
			strSql.Append(" FROM supplyplan ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperOleDb.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM supplyplan ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.ID desc");
			}
			strSql.Append(")AS Row, T.*  from supplyplan T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperOleDb.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			OleDbParameter[] parameters = {
					new OleDbParameter("@tblName", OleDbType.VarChar, 255),
					new OleDbParameter("@fldName", OleDbType.VarChar, 255),
					new OleDbParameter("@PageSize", OleDbType.Integer),
					new OleDbParameter("@PageIndex", OleDbType.Integer),
					new OleDbParameter("@IsReCount", OleDbType.Boolean),
					new OleDbParameter("@OrderType", OleDbType.Boolean),
					new OleDbParameter("@strWhere", OleDbType.VarChar,1000),
					};
			parameters[0].Value = "supplyplan";
			parameters[1].Value = "ID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperOleDb.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

